package com.encon.projecttracker.server.services.impl;


import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.util.Assert;

import com.encon.projecttracker.rpc.beans.TestDTO;
import com.encon.projecttracker.server.data.pojo.Test;
import com.encon.projecttracker.server.services.DiscoveryService;
import com.encon.projecttracker.server.services.QueryService;
import com.encon.projecttracker.server.services.SessionService;

/**
 * 
 * Query service, fetches the result from hibernate and turns it into a list of TestDTO objects.
 *
 * @author tom(at)wamonline.org.uk
 *
 */
public class QueryServiceImpl extends AbstractService 
implements QueryService{

    private SessionService sessionService = null;
    
    private DiscoveryService discoveryService = null;

    public void setDiscoveryService(final DiscoveryService discoveryService) {
        this.discoveryService = discoveryService;
    }
    public void setSessionService(final SessionService service) {
        this.sessionService = service;
    }

    
    public void afterPropertiesSet() throws Exception {
    	Assert.notNull(this.discoveryService);
	Assert.notNull(this.sessionService);
    }

    /* (non-Javadoc)
	 * @see com.encon.projecttracker.rpc.Session#getRecords()
	 */
	public List<TestDTO> getRecords(String userId, String sessionId) {
	this.sessionService.validateSession(userId, sessionId);
	List<TestDTO> results = null;
	Test test = this.testManager.getTest("1");

		results = new ArrayList<TestDTO>();
		//for ( Iterator<?> it = test.iterator(); it.hasNext(); )
		//{
			//Test pupil = (Test) it.next();
			
			TestDTO dto = new TestDTO();
			
			try {
				BeanUtils.copyProperties(dto, test);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			results.add(dto);
		//}

		
		return results;
	}
}
